package com.ws.webpacket.activity;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.ws.webpacket.R;

/**
 *  浮动页面窗口   暂时未使用
 */
public class GameFloatView extends Service {

    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private boolean isShow = false;

    private LocalBroadcastManager broadcastManager;
    public static final String BC_SERVER_STARTED  = "com.ws.webpacket.activity.serverStarted";

    private LinearLayout rootFloat;
    private FrameLayout floatView;
    private View v_Touch01,v_Touch02,v_Touch03,v_Touch04,v_Touch05;

    @Nullable
    @Override
    public IBinder onBind (Intent intent) {
        return null;
    }

    @Override
    public void onCreate () {
        super.onCreate();

        broadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter =new IntentFilter();
        intentFilter.addAction(MainActivity.BC_SHOWFLOATING);
        broadcastManager.registerReceiver(serverBroadcase,intentFilter);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.width = 480;
        layoutParams.height = 800;
        layoutParams.x = 100;
        layoutParams.y = 100;

        initFloatView();
    }

    @Override
    public int onStartCommand (Intent intent, int flags, int startId) {
        Intent bc_Intent = new Intent();
        bc_Intent.setAction(BC_SERVER_STARTED);
        broadcastManager.sendBroadcast(bc_Intent);
        return START_STICKY_COMPATIBILITY;
    }

    private BroadcastReceiver serverBroadcase =new BroadcastReceiver() {
        @Override
        public void onReceive (Context context, Intent intent) {
            String action = intent.getAction();
            switch (action){
                case MainActivity.BC_SHOWFLOATING:
                    showFloatView();
                    break;
            }
        }
    };

    private void initFloatView(){
        rootFloat = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.view_gamefloat,null);
        floatView = rootFloat.findViewById(R.id.frameLayout_FloatView);

        v_Touch01 = rootFloat.findViewById(R.id.view_FloatView_Touch01);
    }

    private void showFloatView(){
        if(MainActivity.getWebLayout() == null){
            Toast.makeText(this, "webLayout is null", Toast.LENGTH_SHORT).show();
            return;
        }
        floatView.addView(MainActivity.getWebLayout());
        windowManager.addView(rootFloat,layoutParams);
        isShow = true;
    }

    public void dismissView(){
        if(isShow){
            rootFloat.removeView(MainActivity.getWebLayout());
            windowManager.removeView(MainActivity.getWebLayout());
        }
    }
}
